
package meteocal.business.control;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import javax.ejb.Stateless;
import java.util.GregorianCalendar;


/**
 *
 * @author luana
 */
public class DateUtils {
    
       
    public Date currentDay(){
        
              
       Date current = new Date();
       
       
       return current;
        
    }
 
   
    
    public Date todayMidnight(){
        
         GregorianCalendar todayMidnight = new GregorianCalendar();
         todayMidnight.set(Calendar.HOUR_OF_DAY, 0);
         todayMidnight.set(Calendar.MINUTE,0);
         todayMidnight.set(Calendar.SECOND, 0);
         todayMidnight.set(Calendar.MILLISECOND, 0);
         
         return todayMidnight.getTime();
         
    }
    
    public String convertDateToString(Date date){
        String dateString;
        String yearS;
        String monthS;
        String dayS;
        
        Calendar dateC = new GregorianCalendar();
        dateC.setTime(date);
        
        Integer year = dateC.get(Calendar.YEAR);
        yearS = year.toString();
        
        /**
         * If month is in between 0 and 9, I need to add a 0 before
         */
        Integer month = dateC.get(Calendar.MONTH) + 1;
        if(month>=1 && month<10){
            monthS = "0"+month.toString();
        } else {
            monthS = month.toString(); 
        }
        
        /**
         * If the day is in between 0 and 9, I need to add a 0 before
         */
        Integer dayOfMonth = dateC.get(Calendar.DAY_OF_MONTH);
        if(dayOfMonth>=1 && dayOfMonth<10){
            dayS = "0"+dayOfMonth.toString();
        } else {
            dayS = dayOfMonth.toString(); 
        }
       
        
        
        dateString = yearS+"-"+monthS+"-"+dayS;
        
        return dateString; 
    }
    public String convertTimeToString(Date time){
        
        String timeString;
        String hoursString;
        String minutesString;
        
        Calendar timeC = selectTimeSlot(time);
        
        /**
         * If hour is in between 00 and 09, I need to add a 0 before
         */
        Integer hour = timeC.get(Calendar.HOUR_OF_DAY);
        if(hour>=0 && hour<10){
            hoursString = "0"+hour.toString();
        } else {
            hoursString = hour.toString(); 
        }
        

       
        /**
         * I need to have minutes in the format "00"
         */
        Integer minutes = timeC.get(Calendar.MINUTE);
        minutesString = minutes.toString()+"0";
        
        timeString = hoursString+":"+minutesString+":"+"00";
        
        return timeString; 
    }
    
    /** 
     * Timeslots: one every 3 hours
     *              00:00 - 02:59 -> 00:00
     *              03:00 - 05:59 -> 03:00
     *              06:00 - 08:59 -> 06:00
     *              09:00 - 11:59 -> 09:00
     *              12:00 - 14:59 -> 12:00
     *              15:00 - 17:59 -> 15:00
     *              18:00 - 20:59 -> 18:00
     *              21:00 - 23:59 -> 21:00.
     * 
     * @param time
     * @return A time corrisponding one of the given timeslots used by the weather forecast
     */
    public Calendar selectTimeSlot(Date time){
        
        Calendar timeC = new GregorianCalendar();
        timeC.setTime(time);
        
        Integer hour = timeC.get(Calendar.HOUR_OF_DAY);
        
        if(hour>=0 && hour <3){
            timeC.set(Calendar.HOUR_OF_DAY, 0);
            timeC.set(Calendar.MINUTE, 00);
        }
        if(hour>=3 && hour <6){
            timeC.set(Calendar.HOUR_OF_DAY,3);
            timeC.set(Calendar.MINUTE, 00);
        }
        if(hour>=6 && hour <9){
            timeC.set(Calendar.HOUR_OF_DAY,6);
            timeC.set(Calendar.MINUTE, 00);
        }
        if(hour>=9 && hour <12){
            timeC.set(Calendar.HOUR_OF_DAY,9);
            timeC.set(Calendar.MINUTE, 00);
        }
        if(hour>=12 && hour <15){
            timeC.set(Calendar.HOUR_OF_DAY, 12);
            timeC.set(Calendar.MINUTE, 00);
        }
        if(hour>=15 && hour <18){
            timeC.set(Calendar.HOUR_OF_DAY, 15);
            timeC.set(Calendar.MINUTE, 00);
        }
        if(hour>=18 && hour <21){
            timeC.set(Calendar.HOUR_OF_DAY, 18);
            timeC.set(Calendar.MINUTE, 00);
        }
        if(hour>=21){
            timeC.set(Calendar.HOUR_OF_DAY, 21);
            timeC.set(Calendar.MINUTE, 00);
        }
        
        return timeC; 
        
    }
    
    /**
     * Get a date and a time and merge them in a single date
     * @param date
     * @param time
     * @return 
     */
    public Date singleDate(Date date, Date time){
        long merge = date.getTime()+time.getTime();
        return new Date(merge);
        
    }
    
        
    /**
    * Get a diff between two dates
    * @param date1 the oldest date
    * @param date2 the newest date
    * @param timeUnit the unit in which you want the diff
    * @return the diff value, in the provided unit
    */
    public long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
        long diffInMillies = date2.getTime() - date1.getTime();
        return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS);
    }
    
    /**
     * Add the date passed as parameter plus i days
     * @param date - date to be sum
     * @param i - days to sum
     * @return - a new Date
     */
    public Date addDays(Date date, Integer i){
        
        Calendar new_date = new GregorianCalendar();
        new_date.setTime(date);
        new_date.add(Calendar.DATE, i);
        
        return new_date.getTime(); 
        
    }
    
}
